/*
 * @Author: moeWang
 * @Date: 2022-03-09 14:36:16
 * @LastEditTime: 2022-03-10 16:42:11
 * @LastEditors: moeWang
 * @Description:
 * @FilePath: \nest-web\src\logical\org\controller\org.controller.ts
 */

import {
  Body,
  Controller,
  Headers,
  Param,
  Post,
  UseGuards,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
import {
  ApiExtraModels,
  ApiOperation,
  ApiSecurity,
  ApiTags,
} from '@nestjs/swagger';
import {
  ApiCommonResponse,
  ApiObjResponse,
  ApiPaginatedResponse,
} from 'src/decorator/api-response.decorator';
import { OrgSearchModel } from 'src/logical/account/models/orgSearch.model';
import { BaseController } from 'src/logical/common/controller/base.controller';
import { BaseListResultModel } from 'src/logical/common/models/BaseListResult.model';
import { BaseResultModel } from 'src/logical/common/models/BaseResult.model';
import { Pagination } from 'src/logical/common/models/Pagination.model';
import { OrgModel } from '../models/org.model';
import { OrgInfoModel } from '../models/orgInfo.model';
import { QueryOrgListModel } from '../models/queryOrgList.model';
import { SelectOrgModel } from '../models/selectOrg.model';
import { ThreeOrgInfoModel } from '../models/threeOrgInfo.model';
import { OrgService } from '../service/org.service';

@ApiSecurity('Authorization')
@ApiTags('组织机构相关接口')
@ApiExtraModels(
  OrgModel,
  OrgInfoModel,
  SelectOrgModel,
  QueryOrgListModel,
  ThreeOrgInfoModel,
)
@Controller('/api/org')
export class OrgController extends BaseController {
  constructor(
    private readonly orgService: OrgService,
    private readonly jwtService: JwtService,
  ) {
    super();
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '查询机构列表' })
  @ApiPaginatedResponse(OrgModel)
  @Post('queryForList')
  async queryForListByOrgType(
    @Body() orgSearchModel: OrgSearchModel,
  ): Promise<BaseListResultModel<Array<OrgModel>>> {
    return this.orgService.queryForListByOrgType(orgSearchModel);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '根据 级别 获取组织机构树结构' })
  @ApiObjResponse(OrgInfoModel)
  @Post('getTreeByLevel')
  async getTreeByLevel(
    @Body() orgSearchModel: OrgSearchModel,
  ): Promise<BaseResultModel<OrgInfoModel>> {
    return this.orgService.getTreeByLevel(orgSearchModel);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '根据父级机构id查询下级机构数据' })
  @ApiPaginatedResponse(OrgModel)
  @Post('queryOrgList')
  async queryOrgList(
    @Body() queryModel: QueryOrgListModel,
  ): Promise<BaseListResultModel<OrgModel>> {
    return this.orgService.queryOrgList(queryModel);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '根据orgId获取组织机构树结构' })
  @ApiObjResponse(OrgInfoModel)
  @Post('queryForTree/:orgId')
  async queryForTree(
    @Param('orgId') orgId: string,
  ): Promise<BaseResultModel<OrgInfoModel>> {
    return this.orgService.queryForTree(orgId);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '根据orgId获取组织机构树结构' })
  @ApiObjResponse(OrgInfoModel)
  @Post('queryForTreeByTwoLevel/:orgId')
  async queryForTreeByTwoLevel(
    @Param('orgId') orgId: string,
  ): Promise<BaseResultModel<OrgInfoModel>> {
    return this.orgService.queryForTreeByTwoLevel(orgId);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '根据orgId获取组织机构详细信息' })
  @ApiObjResponse(OrgInfoModel)
  @Post('getInfo/:orgId')
  async getinfo(
    @Param('orgId') orgId: string,
  ): Promise<BaseResultModel<OrgInfoModel>> {
    return this.orgService.detail(orgId);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '新建/更新' })
  @ApiCommonResponse()
  @Post('save')
  async save(
    @Headers('authorization') token,
    @Body() detail: OrgModel,
  ): Promise<BaseResultModel<any>> {
    const tokenInfo = this.jwtService.decode(token);
    return this.orgService.save(detail, tokenInfo);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '删除' })
  @ApiCommonResponse()
  @Post('delete/:orgId')
  async delete(@Param('orgId') orgId: string): Promise<BaseResultModel<any>> {
    return this.orgService.delete(orgId);
  }
}
